package io.github.maojian.bianrysearch;

import org.junit.Test;

/**
 * @Auther: jian.mao
 * @Date: 2022/5/21 10:40
 * @Description: 第一个错误的版本
 *  https://leetcode.cn/problems/first-bad-version/
 */
public class Q278 {
    public int firstBadVersion(int n) {
        if (isBadVersion(1)) return 1;
        int left = 2, right = n;
        while (left < right) {
            int mid = (right - left) / 2 + left;
            if (isBadVersion(mid) && !isBadVersion(mid - 1)) {
                return mid;
            } else if (isBadVersion(mid)) {
                right = mid - 1;
            } else if (!isBadVersion(mid)) {
                left = mid + 1;
            }
        }
        return left;
    }

    @Test
    public void test() {
        System.out.println(firstBadVersion(2126753390));
    }

    public boolean isBadVersion(int n) {
        int target = 1702766719;
        if (n >= target) {
            return true;
        } else {
            return false;
        }
    }
}
